﻿using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace HenhouseEnvironmentCtrl
{
    public partial class EditHenhouseForm : Form
    {
        private int henhouseId;

        public EditHenhouseForm(int id)
        {
            InitializeComponent();
            henhouseId = id;
            LoadHenhouseDetails();
        }

        private void LoadHenhouseDetails()
        {
            using (SqlConnection conn = DBConnection.CreateConnection())
            {
                DBConnection.Open(conn);
                string query = "SELECT HenhouseName, TempMax, TempMin, HumMax, HumMin, PM25Max, PM25Min, NH3Max, NH3Min, CO2Max, CO2Min FROM Henhouse WHERE HenhouseId = @HenhouseId";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@HenhouseId", henhouseId);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    txtHenhouseName.Text = reader["HenhouseName"].ToString();
                    txtTempMax.Text = reader["TempMax"].ToString();
                    txtTempMin.Text = reader["TempMin"].ToString();
                    txtHumMax.Text = reader["HumMax"].ToString();
                    txtHumMin.Text = reader["HumMin"].ToString();
                    txtPM25Max.Text = reader["PM25Max"].ToString();
                    txtPM25Min.Text = reader["PM25Min"].ToString();
                    txtNH3Max.Text = reader["NH3Max"].ToString();
                    txtNH3Min.Text = reader["NH3Min"].ToString();
                    txtCO2Max.Text = reader["CO2Max"].ToString();
                    txtCO2Min.Text = reader["CO2Min"].ToString();
                }
                DBConnection.Close(conn);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateInputs(out string errorMessage))
            {
                MessageBox.Show(errorMessage, "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string henhouseName = txtHenhouseName.Text;
            float tempMax = float.Parse(txtTempMax.Text);
            float tempMin = float.Parse(txtTempMin.Text);
            float humMax = float.Parse(txtHumMax.Text);
            float humMin = float.Parse(txtHumMin.Text);
            int pm25Max = int.Parse(txtPM25Max.Text);
            int pm25Min = int.Parse(txtPM25Min.Text);
            int nh3Max = int.Parse(txtNH3Max.Text);
            int nh3Min = int.Parse(txtNH3Min.Text);
            int co2Max = int.Parse(txtCO2Max.Text);
            int co2Min = int.Parse(txtCO2Min.Text);

            using (SqlConnection conn = DBConnection.CreateConnection())
            {
                DBConnection.Open(conn);
                string query = "UPDATE Henhouse SET HenhouseName = @HenhouseName, TempMax = @TempMax, TempMin = @TempMin, HumMax = @HumMax, HumMin = @HumMin, " +
                               "PM25Max = @PM25Max, PM25Min = @PM25Min, NH3Max = @NH3Max, NH3Min = @NH3Min, CO2Max = @CO2Max, CO2Min = @CO2Min " +
                               "WHERE HenhouseId = @HenhouseId";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@HenhouseName", henhouseName);
                cmd.Parameters.AddWithValue("@TempMax", tempMax);
                cmd.Parameters.AddWithValue("@TempMin", tempMin);
                cmd.Parameters.AddWithValue("@HumMax", humMax);
                cmd.Parameters.AddWithValue("@HumMin", humMin);
                cmd.Parameters.AddWithValue("@PM25Max", pm25Max);
                cmd.Parameters.AddWithValue("@PM25Min", pm25Min);
                cmd.Parameters.AddWithValue("@NH3Max", nh3Max);
                cmd.Parameters.AddWithValue("@NH3Min", nh3Min);
                cmd.Parameters.AddWithValue("@CO2Max", co2Max);
                cmd.Parameters.AddWithValue("@CO2Min", co2Min);
                cmd.Parameters.AddWithValue("@HenhouseId", henhouseId);
                cmd.ExecuteNonQuery();
                DBConnection.Close(conn);
            }

            MessageBox.Show("鸡舍更新成功！");
            this.Close();
        }

        private bool ValidateInputs(out string errorMessage)
        {
            errorMessage = "";

            if (string.IsNullOrWhiteSpace(txtHenhouseName.Text))
            {
                errorMessage = "鸡舍名称不能为空";
                return false;
            }

            if (!float.TryParse(txtTempMax.Text, out _))
            {
                errorMessage = "请输入有效的最大温度";
                return false;
            }

            if (!float.TryParse(txtTempMin.Text, out _))
            {
                errorMessage = "请输入有效的最小温度";
                return false;
            }

            if (!float.TryParse(txtHumMax.Text, out _))
            {
                errorMessage = "请输入有效的最大湿度";
                return false;
            }

            if (!float.TryParse(txtHumMin.Text, out _))
            {
                errorMessage = "请输入有效的最小湿度";
                return false;
            }

            if (!int.TryParse(txtPM25Max.Text, out _))
            {
                errorMessage = "请输入有效的最大PM2.5值";
                return false;
            }

            if (!int.TryParse(txtPM25Min.Text, out _))
            {
                errorMessage = "请输入有效的最小PM2.5值";
                return false;
            }

            if (!int.TryParse(txtNH3Max.Text, out _))
            {
                errorMessage = "请输入有效的最大NH3值";
                return false;
            }

            if (!int.TryParse(txtNH3Min.Text, out _))
            {
                errorMessage = "请输入有效的最小NH3值";
                return false;
            }

            if (!int.TryParse(txtCO2Max.Text, out _))
            {
                errorMessage = "请输入有效的最大CO2值";
                return false;
            }

            if (!int.TryParse(txtCO2Min.Text, out _))
            {
                errorMessage = "请输入有效的最小CO2值";
                return false;
            }

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EditHenhouseForm_Load(object sender, EventArgs e)
        {

        }
    }
}
